// 模拟订单数据
let orders = [];

// 订单服务类
class OrderService {
  // 创建订单
  createOrder(userId, cartItems, totalAmount, shippingAddress, paymentMethod) {
    return new Promise((resolve) => {
      setTimeout(() => {
        // 生成订单ID
        const orderId = Date.now().toString();
        
        // 创建订单
        const order = {
          id: orderId,
          userId,
          items: cartItems,
          totalAmount,
          shippingAddress,
          paymentMethod,
          status: 'pending', // pending, processing, shipped, delivered, cancelled
          createdAt: new Date().toISOString()
        };
        
        // 添加到订单列表
        orders.push(order);
        
        resolve(order);
      }, 800);
    });
  }
  
  // 获取用户订单
  getUserOrders(userId) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const userOrders = orders.filter(order => order.userId === userId);
        resolve(userOrders);
      }, 400);
    });
  }
  
  // 获取订单详情
  getOrderById(orderId) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const order = orders.find(order => order.id === orderId);
        if (order) {
          resolve(order);
        } else {
          reject(new Error('订单不存在'));
        }
      }, 300);
    });
  }
  
  // 更新订单状态
  updateOrderStatus(orderId, status) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const orderIndex = orders.findIndex(order => order.id === orderId);
        if (orderIndex !== -1) {
          orders[orderIndex].status = status;
          resolve(orders[orderIndex]);
        } else {
          reject(new Error('订单不存在'));
        }
      }, 300);
    });
  }
}

export default new OrderService();
